package com.bw.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.cms.EduWebsiteTemplate;
import com.bw.finance.service.IEduWebsiteTemplateService;
import com.bw.finance.utils.PhotoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxx
 * @since 2020-08-16
 */
@RestController
@RequestMapping("/template")
public class EduWebsiteTemplateController {
    @Autowired
    private IEduWebsiteTemplateService templateService;

    private static final Logger log= LoggerFactory.getLogger(EduWebsiteTemplateController.class);

    @PostMapping("/templatelist")
    public ResponseResult adlist(@RequestBody EduWebsiteTemplate websiteTemplate){
        log.info("网站模板查询开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper<EduWebsiteTemplate> queryWrapper=new QueryWrapper();
            queryWrapper.lambda().eq(EduWebsiteTemplate::getIsDel,0);
            if(websiteTemplate.getName()!=null){
                queryWrapper.lambda().eq(EduWebsiteTemplate::getName,websiteTemplate.getName());
            }
            List<EduWebsiteTemplate> list = templateService.list(queryWrapper);
            result.setResult(list);
        }catch (Exception e){
            log.error("网站模板查询异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站模板查询异常"+e.toString());
        }
        log.info("网站模板查询结束");
        return result;
    }

    @GetMapping("/updatestatus/{id}/{uid}")
    public ResponseResult updatestatus(@PathVariable Integer id,@PathVariable Long uid){
        log.info("网站广告状态修改开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EduWebsiteTemplate websiteTemplate = templateService.getById(id);
            websiteTemplate.setStatus(1);
            websiteTemplate.setUpdateTime(new Date());
            websiteTemplate.setUpdateUser(uid);
            templateService.saveOrUpdate(websiteTemplate);
            result.setMessage("修改成功！！！");
        }catch (Exception e){
            log.error("网站广告状态修改异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站广告状态修改异常"+e.toString());
        }
        log.info("网站广告状态修改结束");
        return result;
    }

    @GetMapping("/del/{id}/{uid}")
    public ResponseResult del(@PathVariable Integer id,@PathVariable Long uid){
        log.info("网站广告删除开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EduWebsiteTemplate websiteTemplate = templateService.getById(id);
            websiteTemplate.setIsDel(1);
            websiteTemplate.setUpdateTime(new Date());
            websiteTemplate.setUpdateUser(uid);
            templateService.saveOrUpdate(websiteTemplate);
            result.setMessage("删除成功！！！");
        }catch (Exception e){
            log.error("网站广告删除异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站广告删除异常"+e.toString());
        }
        log.info("网站广告删除结束");
        return result;
    }

    @PostMapping("/upload/{uid}")
    public ResponseResult upload(MultipartFile file,@PathVariable Long uid){
        log.info("上传模板开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            String name = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));
            String path = PhotoUtils.upload(file);
            EduWebsiteTemplate template = new EduWebsiteTemplate();
            template.setName(name);
            template.setPath(path);
            template.setCreateTime(new Date());
            template.setCreateUser(uid);
            template.setIsDel(0);
            template.setStatus(0);
            templateService.saveOrUpdate(template);
            result.setMessage("上传成功！！！！");
        }catch (Exception e){
            log.error("上传模板异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("上传异常"+e.toString());
        }
        log.info("上传模板结束");
        return result;
    }

    @GetMapping("/temlist")
    public ResponseResult temlist(){
        log.info("模板选择查询开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper<EduWebsiteTemplate> queryWrapper=new QueryWrapper();
            queryWrapper.lambda().eq(EduWebsiteTemplate::getIsDel,0)
                    .eq(EduWebsiteTemplate::getStatus,1);
            List<EduWebsiteTemplate> list = templateService.list(queryWrapper);
            result.setResult(list);
        }catch (Exception e){
            log.error("模板选择查询异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("模板选择查询异常"+e.toString());
        }
        log.info("模板选择查询结束");
        return result;
    }
}
